package com.bwie.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbMenu;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.MD5Utils;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtils;
import com.bwie.vo.LoginVo;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author Lenovo
* @description 针对表【tb_user(用户表)】的数据库操作Service实现
* @createDate 2025-06-19 18:56:16
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{

    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R login(LoginVo loginVo) {
        TbUser tbUser = tbUserMapper.selectOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName, loginVo.getUserName()));
        if(tbUser==null){
            return R.error().setMsg("用户不存在");
        }
        String encrypt = MD5Utils.encrypt(loginVo.getUserPass());
        if(!tbUser.getUserPass().equals(encrypt)){
            return R.error().setMsg("密码错误");
        }
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456").claim("userId", tbUser.getUserId()).claim("userName",tbUser.getUserName()).compact();
        redisTemplate.opsForValue().set("token-"+tbUser.getUserId(),token,60, TimeUnit.MINUTES);
        tbUser.setToken(token);
        return R.ok().setData(tbUser);
    }

    @Override
    public R getMenuByUserId() {
        String token = request.getHeader("token");
        Integer userId = TokenUtils.getUserId(token);
        List<TbMenu> menuByUserId = tbUserMapper.getMenuByUserId(userId);

        List<Tree<String>> treeList = TreeUtil.build(menuByUserId, "0", (menu, tree) -> {
            tree.setName(menu.getMenuName());
            tree.setParentId(menu.getMenuPid()+"");
            tree.setId(menu.getMenuId()+"");
            tree.putExtra("path",menu.getMenuPath());
        });
        return R.ok().setData(treeList);
    }

    @Override
    public R getUserList(PageInfoVo pageInfoVo) {
        Page<TbUser> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        LambdaQueryWrapper<TbUser> wrapper = new QueryWrapper<TbUser>().lambda().orderByDesc(TbUser::getCreateTime);
        Page<TbUser> tbUserPage = tbUserMapper.selectPage(page, wrapper);
        return R.ok().setData(tbUserPage);
    }
}




